import { Fish } from "./Fish";
import { BaseUtil } from "../util/BaseUtil";
import { BaseValue } from "../data/BaseValue";
import { Constants } from "../data/Constants";


const { ccclass, property } = cc._decorator;

@ccclass
export class FishManager extends cc.Component {

    @property({
        type: cc.Prefab
    })
    fishPre: cc.Prefab = null;

    onLoad() {
        for (let i = 0; i < BaseValue.POOL_SIZE; i++) {
            let fish = cc.instantiate(this.fishPre);
            BaseValue.FISH_POOL.put(fish);
        }
    }

    /**
     * 创建鱼
     * @param num  每次创建的数量
     */
    public createFish(num: number) {
        // 使用冰冻技能则停止创建鱼
        if (BaseValue.IS_FROZEN) {
            return;
        }
        for (let i = 0; i < num; i++) {
            let fish = null;
            if (BaseValue.FISH_POOL.size() > 0) {
                fish = BaseValue.FISH_POOL.get();
            } else {
                fish = cc.instantiate(this.fishPre);
            }
            fish.parent = this.node;
            fish.getComponent(Fish).setEntry(BaseUtil.randomNum(1, 23));
        }
    }

    /**
     * 获取节点下的所有鱼
     */
    private _getAllFish() {
        return this.node.children;
    }

    /**
     * 控制鱼的运动
     * @param isRunning 
     */
    public controlMoving(isRunning = true) {
        let allFish = this._getAllFish();
        for (let i = 0; i < allFish.length; i++) {
            const fish = allFish[i].getComponent(Fish);
            if (isRunning) {
                fish.node.resumeAllActions();
            } else {
                if (fish.getFishStatus() === Constants.FishStatus.ALIVE) {
                    fish.node.pauseAllActions();
                }
            }
        }
    }

}
